/*
 * $Header: /home/cvs/jakarta-commons/httpclient/src/examples/TrivialApp.java,v 1.13.2.1 2004/02/22 18:21:12 olegk Exp $
 * $Revision: 1.13.2.1 $
 * $Date: 2004/02/22 18:21:12 $
 *
 * ====================================================================
 *
 *  Copyright 2002-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 * [Additional notices, if required by prior licensing conditions]
 *
 */

import java.io.IOException;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;

public abstract class AbstractSearch {

  private boolean cancelled;
  public final boolean isCancelled() {
    return cancelled;
  }
  public final void cancel() {
    this.cancelled = true;
  }

  boolean debug = true;
  void note(Object msg) {if (debug) System.err.println(msg);}
  void out(Object msg) {System.out.println(msg);}
    
  protected final void handle(Exception e) {e.printStackTrace();}  

  HttpClient client;
  Credentials creds;  


  public static class Request {
    private final HttpMethod method;
    Request(HttpMethod method) {this.method = method;}
    InputStream inStream() throws Exception {return method.getResponseBodyAsStream();}
    BufferedReader reader() throws Exception {
      return new BufferedReader(new InputStreamReader(inStream()));
    }
    void cleanUp() {
      if (method != null) {
        //clean up the connection resources
        method.releaseConnection();
        method.recycle();
      }
    }
  }
    
  Request request(String url) {
    return request(url,null);
  }

  Request request(String url, Map params) {

    boolean isPost = params != null;

    note("request(" + isPost + "): " + url);

    HttpMethod method = null;
    if (isPost) {
      method = new PostMethod(url);
      for (Iterator it = params.keySet().iterator(); it.hasNext();) {
        Object k = it.next();
        String key = String.valueOf(k);
        String val = String.valueOf(params.get(k));
        ((PostMethod)method).addParameter(key,URLEncoder.encode(val));
      }
    } else {
      method = new GetMethod(url);
      method.setFollowRedirects(true);
    }
    method.setStrictMode(false);    
    String result = null;
    try{
      client.executeMethod(method);
    } catch (HttpException he) {
      note("Http error connecting to '" + url + "'");
      note(he.getMessage());
      System.exit(-4);
    } catch (IOException ioe){
      note("Unable to connect to '" + url + "'");
      System.exit(-3);
    }

    Request req = new Request(method);
    
    //return result;
    return req;
  }

}
